<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!--修改课程信息 弹出框-->

<div class="row">
	<div class="col-md-12 col-lg-12 editContent">
		<div class="row">
			<div class="col-md-2 col-lg-2"></div>
			<div class="col-md-8 col-lg-8">
				<div class="newCourse-label">课程基本信息</div>
				<form>
					<input type="hidden" name="id" />
					<div class="form-group">
						<div class="row">
							<div class="col-md-4 col-lg-4">
								<label for="courseName">课程名</label>
							</div>
							<div class="col-md-6 col-lg-6">
								<input type="text" class="form-control" name="name">
							</div>
						</div>
					</div>
					<div class="form-group">
						<div class="row">
							<div class="col-md-4 col-lg-4">
								<label for="courseCode">课程代码</label>
							</div>
							<div class="col-md-6 col-lg-6">
								<input type="text" class="form-control" name="courseCode">
							</div>
						</div>
					</div>
					<div class="form-group">
						<div class="row">
							<div class="col-md-4 col-lg-4">
								<label for="time">课时</label>
							</div>
							<div class="col-md-6 col-lg-6">
								<input type="text" class="form-control" name="courseHour" onkeyup='this.value=this.value.replace(/\D/gi,"")'>
							</div>
						</div>
					</div>
					<div class="form-group">
						<div class="row">
							<div class="col-md-4 col-lg-4">
								<label for="credit">学分</label>
							</div>
							<div class="col-md-6 col-lg-6">
								<input type="text" class="form-control" name="credit">
							</div>
						</div>
					</div>
					<div class="form-group">
						<div class="row">
							<div class="col-md-4 col-lg-4">
								<label for="totalWeek">总周数</label>
							</div>
							<div class="col-md-6 col-lg-6">
								<input type="text" class="form-control" name="sumWeeks" onkeyup='this.value=this.value.replace(/\D/gi,"")'>
							</div>
						</div>
					</div>
					<div class="form-group">
						<div class="row">
							<div class="col-xs-4">
								<label for="description" style="font-size: 1.6rem;font-weight: normal;">课程图片</label>
								<img src="" id="img" style="cursor:pointer;max-width:100%;min-height:3rem;">
								<input type="file" name="file" id="file-name" style="display:none;"/>
								<input type="hidden"  name="fileKey" />
							</div>
							<div class="col-xs-8">
								<div class="row">
									<div class="col-xs-12">
										<label for="description">课程描述</label>
									</div>
									<div class="col-xs-12">
										<div id="description-edit" name="description" contenteditable="true" ></div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
			<div class="col-md-2 col-lg-2"></div>
		</div>
		<div class="col-md-12" style="text-align:center;margin-bottom:2rem;">
			<button class="btn btn-primary" id="updateCourse">确定</button>
		</div>
	</div>
</div>

<script>
var keyList= [];
var lastKey = "";
$(function(){
	$("#img").click(function() {		
		$("#file-name").click();
		$("#file-name").change(function(){
			var objUrl = getCIObjectURL(this.files[0]) ;
		    if (objUrl) {
		    	var key = $("input[name='fileKey']").val();
		    	if(key != ""|| key!=lastKey){
		    		lastKey = key;
		    		keyList.push(lastKey);
		    	}				
		        $("#img").attr("src", objUrl);
		        ajaxCIFileUpload();
		    }
		});
	});
	
});

//图片预览
function getCIObjectURL(file) {
    var url = null ; 
    if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
    } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}
	
function ajaxCIFileUpload() {
	$.ajaxFileUpload({
		url : "<c:url value='/resource/upload'/>", //用于文件上传的服务器端请求地址
		secureuri : false, //是否需要安全协议，一般设置为false
		fileElementId : 'file-name', //文件上传域的ID
		dataType : 'json', //返回值类型 一般设置为json
		success : function(data, status) //服务器成功响应处理函数
		{
			if(data.status=="success"){
				$("input[name='fileKey']").val(data.data.key);
			}else{
				alert(data.message);
			}			 
		},
		error : function(data, status, e)//服务器响应失败处理函数
		{
			alert(e);
		}
	})
	return false;
}

var removeUsing = function(){
	if(keyList.length > 0){
		keyList.push($("input[name='fileKey']").val());
		return keyList.slice(1);
	}else{
		return [];
	}
}
var deleteBefore = function(list){
	
	if(list.length > 0){
		
		$.post(CONTEXT_PATH+"resource/delete?list="+list, function(ddata) {
			if(ddata.status=="success"){
		  		console.log("delete success");
			}else{
				layer.msg("delete wrong");
			}
		});
		
	}
}

$("#updateCourse").click(function(){
	
	var id=data.data.courseId;
	var courseCode=$("input[name='courseCode']").val();
	var name=$("input[name='name']").val();
	var courseHour=$("input[name='courseHour']").val();
	var credit=$("input[name='credit']").val();
	var sumWeeks=$("input[name='sumWeeks']").val();
	var description=$("#description-edit").text();
	var image=$("input[name='fileKey']").val();
	var tid=${sessionScope.id};
	/* delete the file uploaded before */
	var json={
			"id":id,
			"courseCode":courseCode,
			"name":name,
			"courseHour":courseHour,
			"credit":credit,
			"sumWeeks":sumWeeks,
			"description":description,
			"image":image,
			"tid":tid
	};
	
	$.post(CONTEXT_PATH+"course/ajaxUpdate", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			deleteBefore(keyList);
	  		window.location.reload();
		}else{
			alert("wrong");
		}
	}); 
});
</script>